<template>
  <div class="pos">
    <el-row>
      <!-- 商品部分 -->
      <el-col :span="14" class="pos-right">
        <!-- top部分 -->
        <!--        <pos-often></pos-often>-->
        <!-- 商品种类部分 -->
        <div class="goods-type">
          <el-tabs tab-position="bottom"
                   type="card"
                   stretch="true"
                   class="type-tabs">

            <el-tab-pane label="咖啡">
              <ul class="cookList">
                <li v-for="item in coffers" @click="add_to_orderList(item)">

                  <el-popover
                      placement="bottom-start"
                      :title="item.name"
                      :width="200"
                      trigger="hover"
                      :content="item.description"
                  >
                    <template #reference>
                      <!--                    <template #content>{{ item.description}}</template>-->
                      <div class="left-Img">
                        <img :src="'src/assets/cofferimg/'+item.image" width="100%"/>
                      </div>
                    </template>
                  </el-popover>
                  <div class="right-text">
                    <span class="foodName">{{ item.name }}</span>
                    <span class="foodPrice">￥{{ item.price }}元</span>
                  </div>

                </li>
              </ul>
            </el-tab-pane>

            <el-tab-pane label="茶饮">
              <ul class="cookList">
                <li v-for="item in teas" @click="add_to_orderList(item)">
                  <el-popover
                      placement="bottom-start"
                      :title="item.name"
                      :width="200"
                      trigger="hover"
                      :content="item.description"
                  >
                    <template #reference>
                      <div class="left-Img">
                        <img :src="'src/assets/cofferimg/'+item.image" width="100%"/>
                      </div>
                    </template>
                  </el-popover>
                  <div class="right-text">
                    <span class="foodName">{{ item.name }}</span>
                    <span class="foodPrice">￥{{ item.price }}元</span>
                  </div>
                  <!--                  </el-tooltip>-->
                </li>
              </ul>
            </el-tab-pane>

            <el-tab-pane label="甜点">
              <ul class="cookList">
                <li v-for="item in cakes" @click="add_to_orderList(item)">
                  <el-popover
                      placement="bottom-start"
                      :title="item.name"
                      :width="200"
                      trigger="hover"
                      :content="item.description"
                  >
                    <template #reference>
                      <div class="left-Img">
                        <img :src="'src/assets/cofferimg/'+item.image" width="100%"/>
                      </div>
                    </template>
                  </el-popover>
                  <div class="right-text">
                    <span class="foodName">{{ item.name }}</span>
                    <span class="foodPrice">￥{{ item.price }}元</span>
                  </div>
                  <!--                  </el-tooltip>-->
                </li>
              </ul>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-col>
      <!-- 订单部分 -->
      <el-col :span="10" id="order-list" class="pos-left">
        <el-affix :offset="20">


          <div class="order-meal">
            <div class="tableItem">
              <el-table
                  :data="displayedItems"
                  fit="true"
              >
                <el-table-column prop="name" label="商品" align="center"></el-table-column>
                <el-table-column label="数量" align="center">
                  <template #default="scope">
                    {{ table_data[scope.$index].num }}
                  </template>
                </el-table-column>
                <el-table-column label="温度" align="center">
                  <template #default="scope">
                    {{ iceList[table_data[scope.$index].ice].label }}
                  </template>
                </el-table-column>
                <el-table-column label="糖度" align="center">
                  <template #default="scope">
                    {{ sugarList[table_data[scope.$index].sugar].label }}
                  </template>
                </el-table-column>
                <el-table-column prop="price" label="单价" align="center"></el-table-column>
                <el-table-column label="操作" fixed="right" align="center">
                  <template #default="scope">
                    <el-button type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-pagination
                  background
                  :current-page="currentPage"
                  :page-size="pageSize"
                  :total="total"
                  layout="prev, pager, next"
                  @current-change="handleCurrentChange"
              />
            </div>
            <div v-show="totalCount!==0">
              <div class="order-total">
                <p><span>总数量：</span><span>{{ totalCount }}</span></p>
                <p><span>总金额：</span><span>{{ totalMoney }} 元</span></p>
              </div>
              <div class="order-buttons">
                <el-button color="#626aef" size="large" @click="checkout()" style="width: 60px">结账</el-button>
                <el-button color="#626aef" size="large" @click="remove()" style="width: 60px">清空</el-button>
              </div>
            </div>
          </div>
        </el-affix>
      </el-col>
    </el-row>

    <el-dialog v-model="dialogFormVisible" :title="select.name" class="dialogItem" width="600px" center>
      <el-form :model="select" class="formItem">
        <el-form-item class="iceItem" label="温度" :label-width="60">
          <el-radio-group v-model="select.ice">
            <el-radio-button :label="0">{{ iceList[0].label }}</el-radio-button>
            <el-radio-button :label="1">{{ iceList[1].label }}</el-radio-button>
            <el-radio-button :label="2">{{ iceList[2].label }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item class="sugarItem" label="糖度" :label-width="60">
          <el-radio-group v-model="select.sugar">
            <el-radio-button :label="0">{{ sugarList[0].label }}</el-radio-button>
            <el-radio-button :label="1">{{ sugarList[1].label }}</el-radio-button>
            <el-radio-button :label="2">{{ sugarList[2].label }}</el-radio-button>
            <el-radio-button :label="3">{{ sugarList[3].label }}</el-radio-button>
            <el-radio-button :label="4">{{ sugarList[4].label }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item class="numItem">
          <el-input-number v-model="select.count" :min="1" :max="10"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button class="but" @click="butExit">取消</el-button>
        <el-button class="but" type="primary" @click="butOK">确定</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>
<script setup>
// 在组件挂载后执行回调函数
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";

const coffers = ref([])
const teas = ref([])
const cakes = ref([])
const iceList = [
  {label: "常温", value: 0},
  {label: "冰", value: 1},
  {label: "热", value: 2}
]
const sugarList = [
  {label: "正常", value: 0},
  {label: "七分", value: 1},
  {label: "五分", value: 2},
  {label: "三分", value: 3},
  {label: "无糖", value: 4}
]

const totalMoney = ref(0)
const totalCount = ref(0)
const table_data = ref([])

const dialogFormVisible = ref(false)
const select = ref({
  productid: 0,
  name: "青梅绿茶",
  price: 0,
  count: 1,
  ice: 1,
  sugar: 3,
})


onMounted(() => {
  getData()
})

//获取当前页数据
const getData = () => {
  axios.get('http://192.168.166.182:8080/product/getAll', {})
      .then(res => {
        console.log(res.data)
        // coffers.value = res.data;
        coffers.value = res.data.filter(item => {
          return item.categoryid === 1
        })
        teas.value = res.data.filter(item => {
          return item.categoryid === 2
        })
        cakes.value = res.data.filter(item => {
          return item.categoryid === 3
        })
      })
      .catch(error => {
        console.error(error);
      });
};

// 分页
let currentPage = 1;// 定义当前页码初始值为1
const pageSize = 3;// 定义每页显示的数据条数为5
let total = ref(table_data.value.length);// 初始化数据总条数
let displayedItems = ref(table_data.value.slice(0, pageSize));//初始化当前页显示数据

function handleCurrentChange(newPage) {
  currentPage = newPage
  const startIndex = (newPage - 1) * pageSize
  displayedItems.value = table_data.value.slice(startIndex, startIndex + pageSize)
}

const add_to_orderList = (item) => {

  // alert(item.price)
  select.value.productid = item.productid
  select.value.name = item.name
  select.value.price = item.price
  select.value.count = 1
  select.value.ice = 0
  select.value.sugar = 0

  dialogFormVisible.value = true
}

//计算总金额、总数
const calTotal = () => {
  totalMoney.value = 0
  totalCount.value = 0
  for (let i = 0; i < table_data.value.length; i++) {
    console.log(table_data.value[i].count)
    totalCount.value = totalCount.value + table_data.value[i].count;
    totalMoney.value = totalMoney.value + table_data.value[i].price * table_data.value[i].count
  }
}

//删除按钮
const remove = () => {
  table_data.value.splice(0, table_data.value.length)
  calTotal()
  handleCurrentChange(1)
}

//结账
const checkout = () => {
  let cid = 0;
  if(window.localStorage.getItem("access-customer")!==null) {
    let dx = JSON.parse(window.localStorage.getItem("access-customer"))
    console.log(dx)
    cid = dx.customer.cid
  }
  axios.put(`http://192.168.166.182:8080/orderForm/addOrder`, table_data.value, {
        params: {
          totalnum: totalCount.value,
          total: totalMoney.value,
          remarks: "",
          cid:cid
        }
      }
  )
      .then(response => {
        // 处理成功响应
        console.log(response)
        window.open(`http://192.168.166.182:8080/alipay/pay?traceNo=${response.data}&totalAmount=${totalMoney.value}`,)

        table_data.value = []
        calTotal()
        handleCurrentChange(1)
        alert('付款成功!')
      })
      .catch(error => {
        // 处理错误
        console.error('添加失败');
        console.error(error);
      });


}

function handleDelete(index, row) {
  total.value = total.value - 1
  totalCount.value = totalCount.value - row.count
  totalMoney.value = totalMoney.value - row.price
  table_data.value.splice(index + (currentPage - 1) * pageSize, 1)
  handleCurrentChange(1)
}

const butExit = () => {
  dialogFormVisible.value = false
}

const butOK = () => {
  table_data.value.push({
    productid: select.value.productid,
    name: select.value.name,
    price: select.value.price,
    num: select.value.count,
    ice: select.value.ice,
    sugar: select.value.sugar,
    sid: null
  })
  total = ref(table_data.value.length)
  totalMoney.value = totalMoney.value + parseFloat(select.value.price) * parseInt(select.value.count)
  // alert(item.price)
  // alert(total)
  // alert("确认添加到订单")
  totalCount.value = totalCount.value + select.value.count;
  handleCurrentChange(1);
  dialogFormVisible.value = false
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  /*background-color: #646cff;*/
}

.pos-right {
  background-color: #fdf6ec;
}

.type-tabs {
  --el-tabs-header-height: 60px;
  /*background-color: #f56c6c;*/
}

.el-tab-pane {
  height: 670px;
  /*background-color: #747bff;*/
}

/*左边*/
.goods-type /deep/ .el-tabs__item {
  font-size: 16px;
}

.goods-type {
  /*margin-top: 40px;*/
  padding: 0 10px;
  box-sizing: border-box;
  /*background-color: #f0f9eb;*/
  height: 100%;
  --el-transfer-panel-body-height: 100%;
}

.cookList {
  margin-top: 30px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  /*--el-tooltip-v2-border-radius: 20px;*/
}

.cookList li {
  width: 30%;
  list-style: none;
  border: 1px solid #e5e9f2;
  overflow: hidden;
  background-color: #fff;
  margin: 1%;
  border-radius: 5px;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  /*height: 100px;*/
}

.cookList li :hover {
  transform: scale(1.05);
}

.left-Img {
  width: 40%;
  height: 100%;
}

.left-Img img {
  display: inline-block;
  width: 100%;
  height: 100%;
}

.right-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.foodName {
  font-size: 1rem;
  padding: 1rem;
  box-sizing: border-box;
  font-weight: 600;
  color: #E6A23C;
}

.foodPrice {
  font-size: 1rem;
  padding: 10px;
  box-sizing: border-box;
}

/*右边*/

.order-total {
  margin-top: 1.5rem;
  display: flex;
  flex-direction: row;
  justify-content: center;
  font-size: 1rem;
}

.order-total p {
  margin-right: 2rem;
}

.order-total p :last-child {
  font-weight: 600;
  font-size: 1rem;
  color: #E6A23C;
}

.el-pagination { /*选择分页组件，默认采取Flex布局*/
  justify-content: center; /*水平方向居中对齐*/
  margin-top: 8px;
}

.tableItem {
  height: 300px;
}

.order-buttons {
  display: flex;
  justify-content: center;
  padding: 40px;
}

.iceItem {
  margin: 20px 0 10px 50px;
}

.sugarItem {
  margin: 20px 0 10px 50px;
}

.numItem {
  margin: 20px 0 10px 100px;
}

.but {
  width: 80px;
}
</style>
